package von.seiji.cn.other;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: SeiJi
 * @DateTime: 2022/1/28 22:39
 * @Description: 此人很懒，未写描述！
 **/
public class HttpClient implements Runnable {


    private String host;
    private Integer port;

    public HttpClient(String host, Integer port) {
        this.host = host;
        this.port = port;
    }


    @Override
    public void run() {
        System.out.println("Client Start...");

        while (true) {    //一直监听，知道收到停止的命令
            Socket client = null;
            try {
                client = new Socket(host, port);
                System.out.println("客户端发送消息：");
                String line = new BufferedReader(
                        new InputStreamReader(System.in)).readLine();//获取输入流
                PrintWriter printWriter = new PrintWriter(
                        client.getOutputStream(), true);    //自动刷新缓存
                printWriter.write(line);
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (client != null) {
                    try {
                        client.close();
                    } catch (IOException e) {
                        System.out.println("客户端 finally 异常:" + e.getMessage());
                    }
                }
            }
        }
    }


}

class Clientluancher {
    public static void main(String[] args) throws IOException {

        HttpClient httpClient = new HttpClient("localhost", 7777);
        new Thread(httpClient).start();

    }
}
